package com.tmeone.erpone.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmeone.erpone.entity.JsonResult;
import com.tmeone.erpone.entity.TCargo;
import com.tmeone.erpone.entity.TWare;
import com.tmeone.erpone.service.ITWareService;
import com.tmeone.erpone.service.ITCargoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@RestController
public class TCargoController {
    @Autowired
    private ITCargoService cargoService;

    @Autowired
    private ITWareService wareService;



    /*//查询所有的货位
    @RequestMapping("/cargos")
    public JsonResult getCargos() {
        List<TCargo> cargos = cargoService.selectAllCargos();
        return new JsonResult(1, cargos);
    }*/
    //分页
    @GetMapping("/cargos")
    public JsonResult getPageCargos(@RequestParam(value = "current",required = false,defaultValue = "1") Integer current,
                                    @RequestParam(value ="pageSize",required = false,defaultValue = "5") Integer pageSize){
        Page<TCargo> page = new Page<>(current, pageSize);
        return new JsonResult(1,cargoService.getPageCargos(page));
    }

    @DeleteMapping("/cargos/{id}")
    public JsonResult delCargosById(@PathVariable String id) {
        boolean removeById = cargoService.removeById(id);
        return new JsonResult(1, removeById);
    }

    @PutMapping("/cargos/{id}/{status}")
    public JsonResult updateCargoStatusById(@PathVariable String id, @PathVariable String status) {
        int i = cargoService.updateCargoStatusById(id, status);
        System.out.println(i);
        return new JsonResult(1, null);
    }

    @PostMapping("/cargos")
    public JsonResult saveCargos(@RequestBody TCargo cargo){
        TCargo tCargo = new TCargo();
        tCargo.setId(UUID.randomUUID().toString().replace("-","").substring(0,10));
        tCargo.setWareId(UUID.randomUUID().toString().replace("-","").substring(0,10));
        tCargo.setWareNo(cargo.getWareNo());
        tCargo.setCargoNo(cargo.getCargoNo());
        cargo.setCreatTime(LocalDateTime.now());
        tCargo.setCreatTime(tCargo.getCreatTime());
        tCargo.setShelvesId(cargo.getShelvesId());
        tCargo.setProduct(cargo.getProduct());
        tCargo.setCreatTime(cargo.getCreatTime());
        tCargo.setStatus(cargo.getStatus());
        cargoService.save(tCargo);
        TWare tWare = new TWare();
        tWare.setId(tCargo.getWareId());
        tWare.setPerson(cargo.getPerson());
        tWare.setTelephone(cargo.getTelephone());
        wareService.save(tWare);
        return new JsonResult(1,null);
    }

    @PutMapping("/cargos")
    public JsonResult updateCargosById( @RequestBody TCargo cargo) {
        TCargo tCargo = new TCargo();
        tCargo.setId(cargo.getId());
        tCargo.setCargoNo(cargo.getCargoNo());
        tCargo.setShelvesId(cargo.getShelvesId());
        tCargo.setProduct(cargo.getProduct());
        tCargo.setCreatTime(cargo.getCreatTime());
        tCargo.setStatus(cargo.getStatus());
        cargoService.updateById(tCargo);
        TWare tWare = new TWare();
        tWare.setId(cargo.getWareId());
        tWare.setPerson(cargo.getPerson());
        tWare.setTelephone(cargo.getTelephone());
        wareService.updateById(tWare);
        return new JsonResult(1,null);
    }

    @GetMapping("/selectCargos/{cargoNo}/{shelvesId}/{status}")
    public JsonResult selectOne(@PathVariable String cargoNo,@PathVariable String shelvesId,@PathVariable String status){
        List<TCargo> tCargos = cargoService.selectByCargoNoAndShelvesIdAndStatus(cargoNo, shelvesId, status);
        return new JsonResult(1,tCargos);
    }
}
